package com.yict.common.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yict.common.dao.impl.CommonDao;
import com.yict.common.entity.PageEntity;

/**
 * 
 * 
 * @author Patrick.Deng
 * 
 */
@Service
public class CommonService<T, PK extends Serializable> {

	protected PageEntity pageEntity;

	@Autowired
	@Qualifier("commonDao")
	protected CommonDao<T, Serializable> commonDao;

	@Transactional(readOnly = false)
	public boolean save(T t) throws Exception {
		return commonDao.save(t);
	}

	@Transactional(readOnly = false)
	public boolean update(T t) throws Exception {
		return commonDao.update(t);
	}
	
	@Transactional(readOnly = false)
	public boolean saveOrUpdate(T t) throws Exception {
		return commonDao.saveOrUpdate(t);
	}
	
	@Transactional(readOnly = false,rollbackFor = {Exception.class})
	public boolean saveOrUpdate(List<T> t) throws Exception {
		return commonDao.saveOrUpdate(t);
	}

	@Transactional(readOnly = false)
	public boolean remove(T t) throws Exception {
		return commonDao.remove(t);
	}

	@Transactional(readOnly = false,rollbackFor = {Exception.class})
	public boolean delete(List<T> t) throws Exception {
		boolean bool = false;
		for (T t2 : t) {
			bool = commonDao.remove(t2);
		}
		return bool;
	}

	@Transactional(readOnly = false,rollbackFor = {Exception.class})
	public boolean remove(List<T> t) throws Exception {
		return commonDao.remove(t);
	}

	public List<T> search(String queryString, Map<String, Object> valueMap, PageEntity page) {
		return commonDao.search(queryString, valueMap, page);
	}
	
	/**
	 * 根据单个属性查找
	 * @param value
	 * @param propertyName
	 * @return
	 */
	public List<T> listByProperty(Object value,String propertyName){
		List<T> list = new ArrayList<T>();
		Map<String,Object> map = new HashMap<String, Object>();
		map.put(propertyName, value);
		try {
			list = commonDao.list(map, null, 0, 0);
		} catch (Exception e) {
			//logger.info(e);
		}
		return list;
	}
	
	/**
	 * 根据属性查找唯一记录
	 * @param value
	 * @param propertyName
	 * @return
	 * @throws Exception
	 */
	public T findByUnique(Object value,String propertyName) {
		T vo = null;
		Map<String,Object> map = new HashMap<String, Object>();
		map.put(propertyName, value);
		List<T> list = commonDao.list(map, null, 0, 0);
		if(list.size() > 1) 
			throw new RuntimeException("与期望的唯一记录不一致，找到多个记录:" + propertyName + "=" + value);
		if(list.size() == 1) vo = list.get(0);
		return vo;
	}
	
	public T findById(Long id) {
		return commonDao.findbyId(id);
	}

	public PageEntity getPageEntity() {
		return pageEntity;
	}

	public void setPageEntity(PageEntity pageEntity) {
		this.pageEntity = pageEntity;
	}

	public CommonDao<T, Serializable> getCommonDao() {
		return commonDao;
	}

	public void setCommonDao(CommonDao<T, Serializable> commonDao) {
		this.commonDao = commonDao;
	}
}
